package LinkList;

public class LinkList01 {
    //初始化
    static class  ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }


    //头结点
    public static ListNode head;


    //原始链表


    public void createList(){
        ListNode node1 = new  ListNode(12);
        ListNode node2 = new  ListNode(23);
        ListNode node3 = new  ListNode(34);
        ListNode node4 = new  ListNode(45);
        ListNode node5 = new  ListNode(56);
        head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;

    }
    //打印链表
    public void display(){
        ListNode cur = head;
        //最后一个也要打印出来
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }




    //计算链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表中
    public boolean contains(int key){
        ListNode cur = head;

        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //返回这个元素的前驱/
    public ListNode  index(int data){
        ListNode del = new ListNode(data);
        ListNode cur = head;

        while(cur.next != null){
            if(cur.next.val == data){
                return cur;
            }
            cur =cur.next;
        }
        return null;
    }


    //头插法

    public void addFirst(int data){
        ListNode del = new ListNode(data);
        del.next = head;
        head = del;
    }



    //尾插法
    public void addLast(int data){
        ListNode cur = head;
        ListNode del = new ListNode(data);

        while(cur.next != null){
            cur = cur.next;
        }
        //此时跳出循环的cur是最后一个节点啦
        cur.next = del;
        del.next = null;
    }



    //在index这个下标处插入数据data

    //先去找到index前一个节点
    public void insert(int index, int data){
        ListNode cur = head;
        ListNode del= new ListNode(data);
        if(index == 0){
            addFirst(data);
        }
        if(index == size()){
            addLast(data);
        }

        if(index < 0 || index > size()){
            System.out.println("index 不合法");
            return ;
        }
        int count = 0;
        while(count != index -1){
            count++;
            cur = cur.next;
        }
        //此时的cur就是index -1的位置处的节点啦

        del.next = cur.next;
        cur.next  = del;

    }



    //删除第一次出现的关键字key

    public void remove(int key){

        if(head.val == key){
            head = head.next;
            return ;
        }

        if( ! contains(key)){
            System.out.println("这个元素不存在");
            return ;
        }
      ListNode cur = index(key);

        ListNode del = cur.next;
        //此时cur是key的前驱
        cur.next = del.next;
        del.next = null;
    }

    //删除所有值为key的节点,只能遍历链表一遍,
    public void reemvoe(int key){
        //cur代表当前的节点

    }

















}
